package com.jk.lesson5;

import com.jk.bean.Dish;
import com.jk.lesson4.MenuTest;

import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Created by Auser on 2016/11/16.
 */
public class lesson5_6 {
    static List<Dish> menu = MenuTest.menu;

    public static void main(String...args){
        test_5_6_1();
        test_5_6_2();
        test_5_6_3();
    }

    /**
     * 原始类型流特化
     */
    static void test_5_6_1(){
        int calories = menu.stream()
                .mapToInt(Dish::getCalories)
                .sum();
        System.out.println(calories);
        calories = menu.stream()
                .map(Dish::getCalories)
                .reduce(0, Integer::sum);
    }

    static void test_5_6_2(){
        IntStream evenNumbers = IntStream.rangeClosed(2, 100)
                .filter(value -> value % 2 == 0);
        System.out.println(evenNumbers.count());

    }

    static void test_5_6_3(){
        Stream<int[]> pythagoreanTriples = IntStream.rangeClosed(1, 100)
                .boxed()
                .flatMap(value ->
                        IntStream.rangeClosed(value, 100)
                        .filter(value1 -> Math.sqrt(value * value + value1 * value1) % 1 == 0)
                        .mapToObj(value1 -> new int[]{value, value1, (int)Math.sqrt(value * value + value1 * value1)})
                );
        pythagoreanTriples.limit(5)
                .map(ints -> ints[0] + "," + ints[1] + "," + ints[2])
                .forEach(System.out::println);

    }
}
